package com.hxkcloud.patient.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.AddressBean;
import com.hxkcloud.patient.utils.BrightnessUtils;
import com.hxkcloud.patient.wheelview.OnWheelScrollListener;
import com.hxkcloud.patient.wheelview.WheelView;
import com.hxkcloud.patient.wheelview.adapter.ArrayWheelAdapter;

import java.util.List;

/**
 * Created by william_sun on 2016-07-06.
 * Description:地址选择
 */
public class AddressSelectorPopWindow extends PopupWindow implements View.OnClickListener, OnWheelScrollListener {

    private Activity context;
    private WheelView wheel_addr;

    private Button bt_cancel;
    private Button bt_sure;

    private List<AddressBean> addressList;
    private String address[] = new String[]{"东", "南", "西", "北", "中", "上", "下"};

    public AddressSelectorPopWindow(Activity context) {
        this.context = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_selectaddress, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_default);
        initViews(rootView);
        initDatas();
        initListener();
    }

    private void initViews(View rootView) {
        wheel_addr = (WheelView) rootView.findViewById(R.id.wheel_selectaddress_add);
        bt_cancel = (Button) rootView.findViewById(R.id.bt_selectaddress_cancel);
        bt_sure = (Button) rootView.findViewById(R.id.bt_selectaddress_sure);
//        wheel_addr.setDrawShadows(false);
//        wheel_addr.setBackgroundColor(ContextCompat.getColor(context, R.color.white));

    }

    private void initDatas() {

//        NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(context, 1970, curYear);
//        numericWheelAdapter1.setLabel(context.getString(R.string.pop_timeSelector_year));
//        wheel_year.setViewAdapter(numericWheelAdapter1);
//        wheel_year.setCyclic(true);//是否可循环滑动
//
//        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(context, 1, 12, "%02d");
//        numericWheelAdapter2.setLabel(context.getString(R.string.pop_timeSelector_month));
//        wheel_month.setViewAdapter(numericWheelAdapter2);
//        wheel_month.setCyclic(true);

//        ArrayWheelAdapter adapter = new ArrayWheelAdapter(context, address);
//        wheel_addr.setViewAdapter(adapter);
        wheel_addr.setCyclic(false);

        //设置显示行数
        wheel_addr.setVisibleItems(5);

    }

    private void initListener() {
        bt_sure.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(context);
            }
        });
    }

    public void show(View parent, List<AddressBean> addressList) {
        this.addressList = addressList;
        address = new String[addressList.size()];
        for (int i = 0; i < addressList.size(); i++) {
            address[i] = addressList.get(i).getName();
        }

        ArrayWheelAdapter adapter = new ArrayWheelAdapter(context, address);
        wheel_addr.setViewAdapter(adapter);
        wheel_addr.setCurrentItem(0);
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.bt_selectaddress_sure:
                if (addressListener != null)
                    addressListener.setAddress(addressList.get(wheel_addr.getCurrentItem()));
                break;
            case R.id.bt_selectaddress_cancel:

                break;
        }
    }

    @Override
    public void onScrollingStarted(WheelView wheel) {

    }

    @Override
    public void onScrollingFinished(WheelView wheel) {

    }

    public interface AddressListener {
        void setAddress(AddressBean address);
    }

    private AddressListener addressListener;

    public void setAddressListener(AddressListener addressListener) {
        this.addressListener = addressListener;
    }
}
